package com.mapbased.cc;

import java.io.*;
import java.util.*;

import org.apache.commons.httpclient.methods.*;
import org.dom4j.*;
import com.mapbased.wfw.*;
import com.mapbased.wfw.PageLoader.*;
import com.mapbased.wfw.ws.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: mapbased.com</p>
 *
 * @author chy_hs(Baily)
 * @version 1.0
 */
public class ContentGetter
      extends Component
{

    ReqSender sender=new ReqSender();
    boolean alwaysCache=false;
    String cache;
    //  long cacheTo=System.currentTimeMillis();

    //  boolean dynamicURL=false;
    public ContentGetter()
    {
        super();
    }

    /**
     * a component can choose to parse an element or not.
     *
     * @param ele Element
     * @param lc PageLoader
     * @return Element or null:if wanna page loader parse the
     *   component,return the ele, else return null
     * @todo Implement this com.mapbased.wfw.Component method
     */
    public Element parseXml(Element ele,LoadContext lc)
    {
        Attribute a=ele.attribute("url");
        if(a!=null)
        {

            String s=a.getValue();
            LoadContext nlc=new LoadContext(lc);
            lc.loader.parseBinding(s,nlc);
            nlc.finish();
            this.segments=nlc.segments.toArray();
//            for(int i=0;i < this.segments.length;i++)
//            {
//                if(!(this.segments[i] instanceof String))
//                {
//                    this.dynamicURL=true;
//                }
//            }

            ele.remove(a);
        }
        else
        {
            throw new java.lang.RuntimeException(
                  "Cannot find url attribute for:"+ele.getName());
        }
        //////////////////////////////
        a=ele.attribute("cache");
        if(a!=null)
        {
            String s=a.getValue();
            ele.remove(a);
            if("always".equalsIgnoreCase(s))
            {
                this.alwaysCache=true;
//                if(this.dynamicURL)
//                {
//                    throw new java.lang.RuntimeException(
//                        "Cannot set always cache to a dynamic URL:" + s);
//                }
            }
            else if("no".equalsIgnoreCase(s))
            {
                this.alwaysCache=false;
            }
            else
            {
                throw new java.lang.RuntimeException("Unknown cache type:"+s);
            }

        }
        return null;
    }

    /**
     * render
     *
     * @param context RenderContext
     * @throws IOException
     * @todo Implement this com.mapbased.wfw.Component method
     */
    public void render(RenderContext rc)
          throws IOException
    {
        if(this.alwaysCache)
        {
            if(this.cache!=null)
            {
                rc.getOutputWriter().write(this.cache);
                return;
            }

        }

        LocalRenderContext lrc=new LocalRenderContext(rc);

        this.defalutRenderBehavior(lrc);

        String url=lrc.getContent();
        if(url.startsWith("/"))
        {
            javax.servlet.http.HttpServletRequest r=rc.getRequest();

            StringBuffer sb=r.getRequestURL();
            sb.setLength(sb.length()-r.getRequestURI().length());
            sb.append(url);
            url=sb.toString();

        }

        GetMethod g=new GetMethod(url);
        g.setFollowRedirects(true);
        // g.getResponseHeader(HttpConstants.__Expires);
        String output=null;
        try
        {
            this.cache=this.sender.getOkContent(g);

            output=this.cache;
        }
        catch(Exception ex)
        {
            output=ex.toString();

        }

        rc.getOutputWriter().write(output);

    }

    /**
     * renderHeader
     *
     * @param context RenderContext
     * @param sb StringBuffer
     * @param m Map
     * @todo Implement this com.mapbased.wfw.Component method
     */
    public void renderHeader(RenderContext context,StringBuffer sb,Map m)
    {
    }
}
